package com.ybb.order.service;

import com.ybb.account.service.MerchantAccountService;
import com.ybb.account.service.UserAccountService;
import com.ybb.order.entity.OrderEnity;
import com.ybb.order.mapper.OrderMapper;
import com.ybb.order.vo.OrderVo;
import com.ybb.stock.service.GoodsStockService;
import com.ybb.stock.vo.StockChangeVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class OrderServiceImpl implements OrderService{

    @Autowired
    private OrderMapper orderMapper;

    @Autowired
    private GoodsStockService stockService;

    @Autowired
    private MerchantAccountService merchantAccountService;

    @Autowired
    private UserAccountService userAccountService;

    public OrderVo getOrder(Integer orderId) {
        OrderEnity orderEnity = orderMapper.selectById(orderId);
        OrderVo vo = new OrderVo();
        vo.setMerchant(orderEnity.getMerchant());
        vo.setUser(orderEnity.getUser());
        vo.setSku(orderEnity.getSku());
        vo.setNum(orderEnity.getNum());
        vo.setPrice(orderEnity.getPrice());
        vo.setId(orderEnity.getId());
        return vo;
    }

    /**
     * 创建未支付状态的订单
     */
    @Transactional
    public Integer createOrderNopay(OrderVo orderVo) {
        //获取当前价格
        Double price = stockService.getGoodsPrice(orderVo.getSku());

        OrderEnity enity = new OrderEnity();
        enity.setSku(orderVo.getSku());
        enity.setNum(orderVo.getNum());
        enity.setUser(orderVo.getUser());
        enity.setMerchant(orderVo.getMerchant());
        enity.setPrice(price);
        enity.setStatus(OrderService.ORDER_STATUS_UNPAY);
        orderMapper.insert(enity);//生成订单
        Integer orderId = enity.getId();

        StockChangeVo stockChangeVo = new StockChangeVo();
        stockChangeVo.setSku(orderVo.getSku());
        stockChangeVo.setNum(orderVo.getNum());
        //减库存并暂存商品
        stockService.checkoutStage(orderVo.getMerchant(),stockChangeVo, orderId);

        return orderId;
    }

    /**
     * 更新订单状态更新为已支付，同时添加更新时间。
     */
    @Transactional
    public void confirmPayOrder(Integer orderId) {
        OrderVo orderVo = getOrder(orderId);
        //转移用户金额到商户账户
        double amount = orderVo.getNum() * orderVo.getPrice();
        userAccountService.enchargeAccount(orderVo.getUser(), 0-amount);
        merchantAccountService.enchargeAccount(orderVo.getMerchant(), amount);

        //确认暂存商品
        stockService.checkoutConfirm(orderVo.getId());
        //修改状态
        orderMapper.updateOrderStatus(orderVo.getId(), OrderService.ORDER_STATUS_PAYED);
    }

    public List<Integer> getTimeOutOrders(Date watermark, Integer status) {
        return orderMapper.getTimeOutOrders(watermark, status);
    }

    @Transactional
    public void cancelOrder(Integer orderId) {
        //取消订单
        orderMapper.updateOrderStatus(orderId, OrderService.ORDER_STATUS_CANCEL);
        //订单商品回库
        stockService.checkoutRollback(orderId);
    }

    public Map<String, Double> orderAmount(Date start, Date end) {
        return orderMapper.getOrdersAmount(start, end, ORDER_STATUS_PAYED);
    }
}
